package com.clz.appdesign.myapp.webview;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.clz.appdesign.appdependence.base.BaseActivity;
import com.clz.appdesign.myapp.R;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CommonWebActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.video_view)
    FrameLayout mFlVideoView;
    @BindView(R.id.progressbar_web)
    ProgressBar mProgressbarWeb;
    @BindView(R.id.web_view)
    X5WebView mWebView;
    @BindView(R.id.afl_footer)
    FrameLayout mAflFooter;
    private String mUrl;

    @Override
    public void initToolbar() {
        ButterKnife.bind(this);

    }

    @Override
    public void initView() {
        initWebView();
    }

    private void initWebView() {
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                return super.shouldOverrideUrlLoading(webView, s);
            }

            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
            }

            @Override
            public void onReceivedError(WebView webView, int i, String s, String s1) {
                super.onReceivedError(webView, i, s, s1);
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int newProgress) {
                if (newProgress == 100) {
                    mProgressbarWeb.setVisibility(View.GONE);
                } else {
                    mProgressbarWeb.setVisibility(View.VISIBLE);
                    mProgressbarWeb.setProgress(newProgress);
                }
            }

            @Override
            public boolean onJsConfirm(WebView webView, String s, String s1, JsResult jsResult) {
                return super.onJsConfirm(webView, s, s1, jsResult);
            }

            View videoView;
            // /////////////////////////////////////////////////////////
            //
            IX5WebChromeClient.CustomViewCallback callback;

            /**
             * 播放网络视频时全屏会被调用的方法
             */
            @Override
            public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
                CommonWebActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                mFlVideoView.setVisibility(View.VISIBLE);
                mWebView.setVisibility(View.GONE);
                mFlVideoView.removeAllViews();
                mFlVideoView.addView(view);
                videoView = view;
                callback = customViewCallback;
            }

            @Override
            public void onHideCustomView() {
                if (callback != null) {
                    callback.onCustomViewHidden();
                    callback = null;
                }
                if (videoView != null) {
                    videoView=null;
                    mFlVideoView.removeAllViews();
                    mFlVideoView.setVisibility(View.GONE);
                }
                CommonWebActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                mWebView.setVisibility(View.VISIBLE);
            }

            @Override
            public boolean onJsAlert(WebView arg0, String arg1, String arg2,
                                     JsResult arg3) {
                /**
                 * 这里写入你自定义的window alert
                 */
                return super.onJsAlert(null, arg1, arg2, arg3);
            }
        });
        WebSettings webSetting = mWebView.getSettings();
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setAppCachePath(this.getDir("appcache", 0).getPath());
        webSetting.setDatabasePath(this.getDir("databases", 0).getPath());
        webSetting.setGeolocationDatabasePath(this.getDir("geolocation", 0)
                .getPath());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        if (mUrl != null) {
            mWebView.loadUrl(mUrl);
        }

    }

    @Override
    public void initData() {

    }

    @Override
    protected void beforeInit() {
        super.beforeInit();
        if (null != getUrl()) {
            mUrl = getUrl();
        }
    }

    public String getUrl() {
        String url = getIntent().getStringExtra("url");
        return url;
    }

    @Override
    public void doBusiness(Context mContext) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mWebView != null && mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public int layoutResID() {
        return R.layout.activity_comm_web;
    }

    /**
     * 如果网页有视频，开启硬件加速
     */
    public void openHardWareAcclearated() {
        if (Build.VERSION.SDK_INT > 11) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        }
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        if (mWebView != null && mWebView.canGoBack()) {
            mWebView.goBack();
        }else {
            finish();
        }
    }
}
